import service from './config.service'
class common{
    static exit(){
        try {
            uni.clearStorageSync();
        } catch (e) {
            console.log('清除失败');
        }
        common.go(3,'/pages/index/index');
    }
    static back(){
        uni.navigateBack();
    }
    static go(type,path){
        switch (type) {
            case 1:
                uni.navigateTo({
                    url:path
                });
                break;
            case 2:
                //关闭当前页面，跳转到应用内的某个页面
                uni.redirectTo({
                    url:path
                });
                break;
            case 3:
                //关闭所有页面，打开到应用内的某个页面。
                uni.reLaunch({
                    url:path
                });
                break;
            case 4:
                uni.switchTab({
                    url:path
                });
                break;
            case 5:
                if (!uni.getStorageSync('token')){
                    uni.showModal({
                        title: '提示',
                        content: '未登录，请先登录',
                        success: (res) => {
                            if (res.confirm) {
                                common.go(1,'/subPack/pages/login/index');
                            } else if (res.cancel) {
                                console.log('用户点击取消');
                            }
                        }
                    });
                } else{
                    common.go(1,path);
                }
                break;
        }
    }
    static formatDate(value) {
        let date = new Date(value);
        //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM; //月补0
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d; //天补0
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h; //小时补0
        let m = date.getMinutes();
        m = m < 10 ? ('0' + m) : m; //分钟补0
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s; //秒补0
        let time = '';
        time = y + '-' + MM + '-' + d + ' ' + h + ':'+ m; //年月日
        return time
    }
    static upLoadImg(url){
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: `${service.baseUrl}/api/admin.common/upload`, // 仅为示例，非真实的接口地址
                filePath: url,
                name: 'file',
                header:{
                    token:uni.getStorageSync('token')
                },
                success: (res) => {
                    resolve(JSON.parse(res.data));
                },
                fail:(err) => {
                    reject(err);
                }
            });
        })
    }

    static checkId(yourArray,id) {
        if(yourArray && yourArray.length > 0){
            return yourArray.some(item => item.id == id);
        }
    }

    // rpx转px
    static rpxToPx(rpx) {
        const screenWidth = uni.getSystemInfoSync().screenWidth
        return (screenWidth * Number.parseInt(rpx)) / 750
    }
}

export default common
